package com.dmatek.uwb.local.algorithm;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.dmatek.uwb.packet.bean.AssistLocation;
import com.dmatek.uwb.packet.bean.ENodeBDistance;
import com.dmatek.uwb.packet.bean.FilterTagInfor;
import com.dmatek.uwb.packet.bean.TagInfor;
import com.dmatek.uwb.xml.dom.bean.BaseNode;
import com.dmatek.uwb.xml.dom.bean.BasePath;
import com.dmatek.uwb.xml.dom.bean.Config;
import com.dmatek.uwb.xml.dom.bean.Divider;
import com.dmatek.uwb.xml.dom.bean.Point;
import com.dmatek.uwb.xml.dom.bean.Region;

/***
 * @author zf
 * @data 2019年6月17日 上午11:06:16
 * @Description
 *   单点定位
 */
public class SingleLocalAlgorithm extends BaseSingleLocalAlgorithm {
	private IFilterHopsModel iFilterHopsModel;
	public SingleLocalAlgorithm(Config config,Map<String, BasePath> pathsMap, Map<String, BaseNode> nodesMap,
			Map<String, Region> regionsMap, Map<String, Divider> dividersMap, IFilterHopsModel iFilterHopsModel)
			throws Exception {
		super(config, pathsMap, nodesMap, regionsMap, dividersMap);
		this.iFilterHopsModel = iFilterHopsModel;
	}
	/***
	 * 计算卡片坐标
	 */
	public AssistLocation calculatePosition(TagInfor tagInfor) throws Exception {
		/** 对卡片上报的基站进行分组：
		 *      1. 匹配到最短距离的基站;
		 * 		2. 没有匹配到最短距离的基站;
		 * 		3. 匹配到主路径上的基站
		 * */ 
		List<ENodeBDistance> matchBranchs = new ArrayList<ENodeBDistance>(), 
				disMatchBranchs = new ArrayList<ENodeBDistance>(), 
				matchMains = new ArrayList<ENodeBDistance>();
		AssistLocation assistLocation = new AssistLocation();
		// 1. 对上报上来的基站进行分组
		dividerENodeBDistances(tagInfor, matchBranchs, disMatchBranchs, matchMains);
		if(isDebug()) { // 是否需要输入调试讯息
			// 匹配走廊基站
			assistLocation.getMainRefers().addAll(matchMains);
			// 匹配未达到极限距离的房间基站
			assistLocation.getDismatchBranchs().addAll(disMatchBranchs);
			// 匹配达到极限距离的房间基站
			assistLocation.getMatchBranchs().addAll(matchBranchs);
		}
		// 2. 匹配到小于距离阀值的基站
		Point place = null;
		if(matchBranchs.size() > 0) {
			place = obtainMatchBranchLocal(matchBranchs, matchMains, disMatchBranchs, assistLocation);
		} else {
		// 3. 没有匹配到距离阀值的基站
			place = obtainMainPosition(matchMains, disMatchBranchs, assistLocation);
		}
		if(null != place && (place.getX() > 0 || place.getY() > 0)) {
			if(null != getiFilterHopsModel()) {
				if(tagInfor instanceof FilterTagInfor) {
					FilterTagInfor filterTagInfor = (FilterTagInfor) tagInfor;
					place = getiFilterHopsModel().filterHopsCoordinate(filterTagInfor, place, assistLocation);
				}
			}
			assistLocation.setPlace(place);
		}
		return assistLocation;
	}
	public IFilterHopsModel getiFilterHopsModel() {
		return iFilterHopsModel;
	}
	public void setiFilterHopsModel(IFilterHopsModel iFilterHopsModel) {
		this.iFilterHopsModel = iFilterHopsModel;
	}
}
